﻿Imports Microsoft.Office.Interop.Outlook

Public Class InspectorHelperClass

    Public Shared InspectorsList As New InspectorWrapperCollection
    Private intID As Integer

    Public Function AddInsp(ByVal Inspector As Outlook.Inspector) As String
        Dim iWrap As New InspectorWrapper()
        Dim itm As Object
        Dim id As String

        On Error Resume Next

        'set the Inspector in the class
        iWrap.Inspector = Inspector

        'test which Outlook item type is here
        itm = Inspector.CurrentItem
        If TypeOf itm Is Outlook.MailItem Then
            iWrap.MailItem = CType(itm, MailItem)
        ElseIf TypeOf itm Is Outlook.ContactItem Then
            iWrap.ContactItem = CType(itm, ContactItem)
        End If
        itm = Nothing

        iWrap.Key = intID
        id = CStr(intID)
        'add the class to the collection with a
        ' unique Key value.
        InspectorsList.Add(iWrap, id)

        'create buttons and menus for the Inspector
        iWrap.InitButton()

        AddInsp = id

        intID = intID + 1

        iWrap = Nothing
        itm = Nothing
    End Function

    Public Sub KillInsp(ByVal intID As Integer, ByVal inspWrap As InspectorWrapper)
        Dim inspWrap2 As InspectorWrapper

        On Error Resume Next

        inspWrap2 = InspectorsList.Item(CStr(intID))
        ' check to make sure we're removing the
        ' correct Inspector from the collection.
        If Not inspWrap2 Is inspWrap Then
            Err.Raise(1, Description:="Unexpected Error in KillInsp")
        Else
            InspectorsList.Remove(CStr(intID))
        End If

        inspWrap2 = Nothing
    End Sub
End Class